#include <stdio.h>
#include "nuclei_sdk_hal.h"
#include "nuclei_libopt.h"
#include "anl_printf.h"

uint8_t read_data=0;
uint8_t receive_data[10]={0};

int main()
{

    
    i2c_init(I2C,0x3,0x3c,0x1);    //trise =6  fltr=1    n = (div +1 )*5 + trise      F(i2c) = F(core)/n
    i2c_enable(I2C);
    while(i2c_flag_get(I2C,BUSY));

    i2c_mode_addr_config(I2C,0x10,I2C_WRITE);	// send "slave addr"+"write" flag.
    i2c_send_start_write_cmd(I2C);	// send "start"+"write" cmd;
    while(i2c_flag_get(I2C,TIP));   // 
    i2c_data_transmit(I2C,0x0);

    i2c_data_transmit(I2C,0x5a);

    i2c_data_write(I2C,0xaa);

    i2c_close_transmit(I2C);

    while(i2c_flag_get(I2C,TIP));
    


    i2c_mode_addr_config(I2C,0x10,I2C_WRITE);

    i2c_send_start_write_cmd(I2C);

    while(i2c_flag_get(I2C,TIP));

    i2c_data_transmit(I2C,0x0);

    i2c_mode_addr_config(I2C,0x10,I2C_READ);

    i2c_send_start_write_cmd(I2C);

    while(i2c_flag_get(I2C,TIP));

    receive_data[0] = i2c_data_receive(I2C);

    i2c_close_receive(I2C);

    while(i2c_flag_get(I2C,TIP));

    receive_data[1] = i2c_data_read(I2C);

    if(receive_data[0]==0x5a && receive_data[1]==0xaa)
    {
        #ifdef CFG_SIMULATION
        pass_fail_simulation(1);
        #endif 
        anl_printf("I2c write/read pass \r\n");
    }else
    {
        #ifdef CFG_SIMULATION
        pass_fail_simulation(0);
        #endif 
        anl_printf("I2C  write/read fail,receive_data[0] : %x,receive_data[1]:%x\r\n",receive_data[0],receive_data[1]);
    }
    
    return 0;
}

